package com.coocaa.ops.api.v1.internal.provider.impl;

import com.coocaa.ops.api.base.cache.enums.InterfaceCacheMethodEnum;
import com.coocaa.ops.api.base.cache.model.SimpleCacheKey;
import com.coocaa.ops.api.base.cache.service.SimpleCacheService;
import com.coocaa.ops.api.v1.content.biz.IPanelInfoBiz;
import com.coocaa.ops.api.v1.content.model.internal.PanelContentInfo;
import com.coocaa.ops.api.v1.internal.mapper.IPanelCacheMapper;
import com.coocaa.ops.api.v1.internal.provider.ICacheUpdateProvider;
import com.coocaa.ops.common.core.biz.cache.constant.CacheTime;
import com.coocaa.ops.common.core.biz.panel.constant.PanelTypeConstants;
import com.coocaa.ops.common.core.biz.panel.entity.BlockContentConfigInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tangminghong
 * @date 2020-08-11
 */
@Service("panel")
public class PanelCacheUpdateProviderImpl implements ICacheUpdateProvider {

    @Autowired
    private SimpleCacheService cacheService;

    @Autowired
    private IPanelInfoBiz panelInfoBiz;

    @Autowired
    private IPanelCacheMapper panelCacheMapper;

    @Autowired
    private TabCacheUpdateProviderImpl tabCacheUpdateProvider;

    /**
     * 更新缓存-通过资源ID
     *
     * @param idStr
     * @return 缓存key列表
     */
    @Override
    public List<String> updateCacheByResourceId(String idStr) {
        Integer id = Integer.valueOf(idStr);
        PanelContentInfo panelContentInfo = panelInfoBiz.getPanelContent(id, null);
        List<String> cacheKeys = new ArrayList<>(4);
        if (panelContentInfo != null) {
            panelContentInfo = handleGroupPanel(panelContentInfo, id);
            SimpleCacheKey cacheKey = getPanelCacheKey(id);
            cacheService.set(cacheKey, panelContentInfo, CacheTime.ONE_DAY);
            cacheKeys.add(cacheKey.toString());
            handleBlockDmp(id, cacheKeys, PanelTypeConstants.isGroup(panelContentInfo.getPanelType()));
            handleTabPanelRls(id, cacheKeys);
        }

        return cacheKeys;
    }

    private PanelContentInfo handleGroupPanel(PanelContentInfo panelContentInfo, Integer panelId) {
        if (PanelTypeConstants.isGroup(panelContentInfo.getPanelType())) {
            return panelInfoBiz.getGroupPanelFirstContent(panelId, null);
        }
        return panelContentInfo;
    }

    private SimpleCacheKey getPanelCacheKey(Integer panelId) {
        return SimpleCacheKey.getCacheKeyInstance(InterfaceCacheMethodEnum.getPanelContent)
                .withFilterKey("panelId", panelId);
    }

    private void handleBlockDmp(Integer panelId, List<String> cacheKeys, boolean groupPanel) {
        List<BlockContentConfigInfo> blockList = panelInfoBiz.getAllDmpBlocks(panelId, null, groupPanel);
        if (blockList != null) {
            SimpleCacheKey cacheKey = SimpleCacheKey.getCacheKeyInstance(InterfaceCacheMethodEnum.getAllDmpBlocks)
                    .withFilterKey("panelId", panelId);
            cacheService.set(cacheKey, blockList, CacheTime.ONE_DAY);
            cacheKeys.add(cacheKey.toString());
        }
    }

    private void handleTabPanelRls(Integer panelId, List<String> cacheKeys) {
        List<Integer> tabIdList = panelCacheMapper.getTabIdListByPanelId(panelId);
        List<Integer> tabIdListForDmp = panelCacheMapper.getTabIdListByPanelIdForPanelDmp(panelId);

        if (CollectionUtils.isNotEmpty(tabIdList)) {
            for (Integer tabId : tabIdList) {
                List<String> keys = tabCacheUpdateProvider.updateRls(tabId);
                cacheKeys.addAll(keys);
            }
        }
        if (CollectionUtils.isNotEmpty(tabIdListForDmp)) {
            for (Integer tabId : tabIdListForDmp) {
                List<String> keys = tabCacheUpdateProvider.updateRls(tabId);
                cacheKeys.addAll(keys);
            }
        }
    }
}
